package com.beerandjesus.webapp.sportofkings.handicapper.struts.action;

import com.beerandjesus.webapp.sportofkings.handicapper.util.FileUtil;
import com.beerandjesus.webapp.sportofkings.handicapper.util.TrackCodeRaceDate;
import com.opensymphony.xwork2.ActionSupport;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Date: 10/19/14
 * Time: 9:52 AM
 */
@Component
public class ListResultsAction extends ActionSupport {


    @Value("${com.beerandjesus.sportofkings.handicapper.results.dir}")
    private String resultsDir;

    private FileUtil fileUtil = new FileUtil();

    private List<TrackCodeRaceDate> trackCodeRaceDates;

    @Override
    public String execute() throws Exception {
        File[] files = new File(resultsDir).listFiles(fileUtil.resultsFilenameFilter());
        trackCodeRaceDates = new ArrayList<TrackCodeRaceDate>(files.length);
        fileUtil.sortByLastModifiedDescending(files);
        int count = 0;
        for (File file : files) {
            if (count++ > 20) {
                break;
            }
            trackCodeRaceDates.add(fileUtil.parseFileName(file.getName()));
        }
        return SUCCESS;
    }

    public void setResultsDir(String resultsDir) {
        this.resultsDir = resultsDir;
    }

    public List<TrackCodeRaceDate> getTrackCodeRaceDates() {
        return trackCodeRaceDates;
    }

    public void setTrackCodeRaceDates(List<TrackCodeRaceDate> trackCodeRaceDates) {
        this.trackCodeRaceDates = trackCodeRaceDates;
    }

}
